package com.tanhua.manage.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.mapper.LogMapper;
import com.tanhua.manage.pojo.Log;

import com.tanhua.manage.vo.LogVo;
import com.tanhua.manage.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogsService extends ServiceImpl<LogMapper, Log> {

    @Autowired
    private LogMapper logMapper;

    /**
     *   获取日志
     *
     * @param page
     * @param pageSize
     * @param uid
     * @param sortOrder
     * @param sortProp
     * @Return
     */
    public PageResult getLogs(Integer page, Integer pageSize, Long uid, String sortProp, String sortOrder) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);

        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid).orderByDesc("created");
        Integer count = this.logMapper.selectCount(queryWrapper);

        IPage<Log> logsIPage = new Page<>(page, pageSize);
        logsIPage = this.logMapper.selectPage(logsIPage, queryWrapper);

        List<Log> records = logsIPage.getRecords();
        List<LogVo> newPages = new ArrayList<>();
        for (Log record : records) {
            LogVo logsVo = new LogVo();
            logsVo.setId(record.getId());
            logsVo.setUserId(uid);
            logsVo.setLogTime(DateUtil.formatDateTime(record.getCreated()));
            logsVo.setPlace(record.getPlace());
            logsVo.setEquipment(record.getEquipment());
            newPages.add(logsVo);
        }
        pageResult.setCounts(count);
        pageResult.setItems(newPages);
        return pageResult;
    }
}
